package com.yimils.core.goods.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yimils.core.goods.entity.ChestEntity;
import com.yimils.core.goods.entity.GoodsChestEntity;
import com.yimils.core.goods.mapper.GoodsChestMapper;
import com.yimils.core.goods.serivce.ChestService;
import com.yimils.core.goods.serivce.GoodsChestService;
import com.yimils.core.goods.vo.GoodsChestRes;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("goodsChestService")
public class GoodsChestServiceImpl extends ServiceImpl<GoodsChestMapper, GoodsChestEntity> implements GoodsChestService {
    @Resource(name = "chestService")
    ChestService chestService;
    @Override
    public Boolean saveGoodsChest(List<Long> chestIds, Long goodsId) {
        List<GoodsChestEntity> collect = chestIds.stream().map(o -> {
            GoodsChestEntity goodsChestEntity = new GoodsChestEntity();
            goodsChestEntity.setChestId(o);
            goodsChestEntity.setGoodsId(goodsId);
            goodsChestEntity.setState(Boolean.FALSE);
            goodsChestEntity.setCreateTime(LocalDateTime.now());
            goodsChestEntity.setUpdateTime(LocalDateTime.now());
            return goodsChestEntity;
        }).collect(Collectors.toList());
        return saveOrUpdateBatch(collect);
    }

    @Override
    public Boolean deleteGoodsChest(List<Long> chestIds, Long goodsId) {
        QueryWrapper<GoodsChestEntity> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(GoodsChestEntity::getGoodsId, goodsId)
                .eq(GoodsChestEntity::getState, Boolean.FALSE);
        List<GoodsChestEntity> entities = list(qw);
        entities.forEach(o -> {
            o.setState(Boolean.TRUE);
            o.setUpdateTime(LocalDateTime.now());
        });

        return saveGoodsChest(chestIds, goodsId);
    }

    @Override
    public Map<Long, List<GoodsChestRes>> mapListGoodsChest(List<Long> goodsIds) {
        QueryWrapper<GoodsChestEntity> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(GoodsChestEntity::getGoodsId, goodsIds)
                .eq(GoodsChestEntity::getState, Boolean.FALSE);
        List<GoodsChestEntity> entities = list(qw);

        List<Long> chestIds = entities.stream().map(GoodsChestEntity::getChestId).collect(Collectors.toList());
        List<ChestEntity> pictureEntities = chestService.listChest(chestIds);
        Map<Long, String> videoMap = pictureEntities.stream().collect(Collectors.toMap(ChestEntity::getId, ChestEntity::getChestName));
        return entities.stream().map(o -> {
            GoodsChestRes res = new GoodsChestRes();
            res.setGoodsId(o.getGoodsId());
            res.setChestId(o.getChestId());
            res.setChestName(videoMap.get(o.getId()));
            return res;
        }).collect(Collectors.groupingBy(GoodsChestRes::getGoodsId));
    }
}
